We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Pass additional parameters to $form->render()

Hei there!

I would like to pass additional parameters to a form. Right now the form renderer takes a value to define the form, and an array to present additional html tags (like css class, etc).

Is it possible to add another array of values?

Lets assume I want to render a list of select boxes for all items I have in a database. The select boxes shall contain all categories/memberships of -each- item from the database.

So I would like to use this template:

{% for item in items_list %}
    {{ form('/controller/action') }}
        <?php echo $form->render(
            "form_name",
            array('class' =>'css-class'),

            /*additional array to pass to form*/
            array('a' => 'value a','b' => 'value b','c' => 'value c')
        ) ?>
    </form>
{% endfor  %}

And now the FormNameForm.php: ??

<?php

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Select;
use Phalcon\Models\MyModel;

class FormName extends Form
{
    // What to do here? :/
}

Is my approach possible? Or is there another way?

greetings

edited Jan '15

Something like this? The view only outputs the end result of the HTML select element, everything else is created in the form class.

class SomeFormName extends Form
{
    /**
     * @param Posts $post
     * @param array $options null | ['edit', 'add']
     */
    public function initialize(Posts $post, $options)
    {
        // POST TYPE
        // allPostTypes returns preprocessed associative array like:
        // [ 'foo' => 'My Foo Type', 'bar => 'My Bar Type' ]
        $allPostTypes = $post->getPostTypes();

        $postType = new Select(
            "post_type",  
            $allPostTypes, 
            ['class' => 'someCss someOtherCss']
        );

        // $options from the construct parameter
        // You may not need this part
        if (isset($options['add'])) {
            // If this is creation, preselect a value
            $postType->setAttribute('value', 'foo');
        }

        $postType->setLabel('Post type:');
        $this->add($postType);

        ...

Posting from my work-account. :)

Yes this looks promising! I'll check this out. :) Thank you.